package bean;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

public class ReqAll {
        /**
         * Desc:
         * http请求方法，比如get、post、options等.
         * http版本，比如http1.0、http1.1
         * http请求头，比如Host、User-Agent
         * */
        public String httpMethod;
        public String uriPath;
        public HashMap<String, String> requsetParams = new HashMap<>();
        public String httpVersion;
        public HashMap<String, String> headers = new HashMap<>();;
        public String httpBody;
        public HashMap<String, String> bodyParams = new HashMap<>();;
        public HashMap<String, String> cookies = new HashMap<>();;

        public ReqAll() {}

        public String getHttpMethod() {
                return httpMethod;
        }

        public void setHttpMethod(String httpMethod) {
                this.httpMethod = httpMethod;
        }

        public String getUriPath() {
                return uriPath;
        }

        public void setUriPath(String uriPath) {
                this.uriPath = uriPath;
        }

        public HashMap<String, String> getRequsetParams() {
                return requsetParams;
        }

        public void setRequsetParams(HashMap<String, String> requsetParams) {
                this.requsetParams = requsetParams;
        }

        public String getHttpVersion() {
                return httpVersion;
        }

        public void setHttpVersion(String httpVersion) {
                this.httpVersion = httpVersion;
        }

        public HashMap<String, String> getHeaders() {
                return headers;
        }

        public void setHeaders(HashMap<String, String> headers) {
                this.headers = headers;
        }

        public String getHttpBody() {
                return httpBody;
        }

        public void setHttpBody(String httpBody) {
                this.httpBody = httpBody;
        }

        public HashMap<String, String> getBodyParams() {
                return bodyParams;
        }

        public void setBodyParams(HashMap<String, String> bodyParams) {
                this.bodyParams = bodyParams;
        }

        public HashMap<String, String> getCookies() {
                return cookies;
        }

        public void setCookies(HashMap<String, String> cookies) {
                this.cookies = cookies;
        }

        /**
         *  Author: m4n1c.4ny0neSec
         *  Date: 2022/2/11
         *  Desc:
         *  请求解析
         * @param requsetStr 请求字符串
         */
        public void analysisRequset(String requsetStr){

                String[] splitContent = requsetStr.trim().split("\n");
                Integer delimiterIndex = splitContent.length-1;
                //判断请求头和请求体分界
                for (int i = 0; i < splitContent.length; i++) {
                        if(StringUtils.isAllBlank(splitContent[i]) && i!=splitContent.length-1){
                                delimiterIndex = i;
                        }
                }


                //解析请求行
                String requestLine = splitContent[0];
                String[] requestLines = requestLine.split(" ");
                httpMethod = requestLines[0];
                uriPath = requestLines[1];
                if(requestLines[1].contains("?")){
                        String[] requestLineUriParams = requestLines[1].substring(requestLines[1].indexOf("?") + 1).split("&");
                        for (int i = 0; i < requestLineUriParams.length; i++) {
                                if(requestLineUriParams[i].contains("=")) {
                                        if(StringUtils.isAllBlank(requestLineUriParams[i])){
                                                continue;
                                        }
                                        String paramKey = requestLineUriParams[i].substring(0, requestLineUriParams[i].indexOf("="));
                                        String paramValue = requestLineUriParams[i].substring(requestLineUriParams[i].indexOf("=")+1);
                                        requsetParams.put(paramKey,paramValue);
                                }else {
                                        continue;
                                }
                        }
                }

                httpVersion = requestLines[2];

                //请求头解析
                for (int i = 1; i <= delimiterIndex; i++) {
                        if(StringUtils.isAllBlank(splitContent[i])){
                                continue;
                        }
                        String headersParamKey = splitContent[i].substring(0, splitContent[i].indexOf(":"));
                        String headersParamValue = splitContent[i].substring(splitContent[i].indexOf(":")+1);
                        if(!headersParamKey.equalsIgnoreCase("Cookie")){
                                headers.put(headersParamKey,headersParamValue);
                        }else {
                                analysisCookie(headersParamValue);
                        }
                }

                //请求体解析
                if(delimiterIndex!=splitContent.length-1){      //存在请求体
                        for (int i = delimiterIndex+1; i < splitContent.length; i++) {
                                if(!StringUtils.isAllBlank(splitContent[delimiterIndex+1])){
                                        httpBody = httpBody + "\n" + splitContent[delimiterIndex+1];
                                        if(splitContent[delimiterIndex+1].contains("=")){
                                                if(splitContent[delimiterIndex+1].indexOf("=")!=0 && splitContent[delimiterIndex+1].indexOf("=")!=splitContent[delimiterIndex+1].length()-1){   //键值对形式
                                                        if(splitContent[delimiterIndex+1].contains("&")){    //考虑多参情况
                                                                String[] splitRequsetBodyParams = splitContent[delimiterIndex + 1].split("&");
                                                                for (int j = 0; j < splitRequsetBodyParams.length; j++) {
                                                                        if(splitRequsetBodyParams[j].contains("=")){
                                                                                String paramKey = splitRequsetBodyParams[j].substring(0, splitRequsetBodyParams[j].indexOf("="));
                                                                                String paramValue = splitRequsetBodyParams[j].substring(splitRequsetBodyParams[j].indexOf("=")+1);
                                                                                bodyParams.put(paramKey,paramValue);
                                                                        }else {
                                                                                continue;
                                                                        }

                                                                }
                                                        }else {        //单一参数
                                                                String paramKey = splitContent[delimiterIndex+1].substring(0, splitContent[delimiterIndex+1].indexOf("="));
                                                                String paramValue = splitContent[delimiterIndex+1].substring(splitContent[delimiterIndex+1].indexOf("=")+1);
                                                                bodyParams.put(paramKey,paramValue);
                                                        }
                                                }
                                        }
                                }else {
                                        continue;
                                }
                        }
                }

        }

        /**
         * CooKie解析
         * @param headersParamValue CookieValue
         */
        public void analysisCookie(String headersParamValue) {
                String[] cookieParams = headersParamValue.split(";");
                for (int i = 0; i < cookieParams.length; i++) {
                        if(StringUtils.isAllBlank(cookieParams[i])){
                                continue;
                        }
                        String cookieParamKey = cookieParams[i].substring(0, cookieParams[i].indexOf("="));
                        String cookieParamValue = cookieParams[i].substring(cookieParams[i].indexOf("=")+1);
                        cookies.put(cookieParamKey,cookieParamValue);
                }
        }
}
